<template>
  <el-dialog title="选择机器" width="400px" :visible.sync="params.visible">
    <compTree
      ref="tree"
      :show-search="false"
      :comp-tree-height="350"
      :tree-show-checkbox="true"
      :tree-data="treeData"
      :tree-default-checked-keys="treeDefaultCheckedKeys"
    />
    <dialog-btn slot="footer" />
  </el-dialog>
</template>

<script>
import { getAgentList } from '@/api/core'
import { ParamArr, SQLTable } from '../../../public/js/paramarr'
import Global from '../../../public/js/global'
export default {
  name: 'SelectComputerLayer',
  props: {
    params: {
      type: Object,
      default() {
        return {
          visible: false,
          agentId: '', // 默认agent id
          computerName: '',
          isNullSelect: false
        }
      }
    }
  },
  data() {
    return {
      treeData: [],
      treeDefaultCheckedKeys: []
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      if (this.params.computerName === undefined) this.params.computerName = ''
      if (this.params.isNullSelect === undefined) this.params.isNullSelect = false
      if (this.params.agentId === undefined) this.params.agentId = ''
      getAgentList(new ParamArr()).then((vPar) => {
        const nodes = []
        const nodeMap = new Map()
        let table = new SQLTable(vPar.getTable('k_agent_group'))
        table.first()
        while (!table.eof()) {
          const pnode = Global.getTreeviewPNode(
            nodeMap,
            nodes,
            table.getValueByName('PID')
          )
          const node = {}
          node['id'] = table.getValueByName('ID')
          node['text'] = table.getValueByName('Name')
          node['icon'] = 'wenjianyi'
          node['type'] = 1
          pnode.push(node)
          nodeMap.set(table.getValueByName('ID'), node)
          table.next()
        }
        table = new SQLTable(vPar.getTable('k_agent'))
        table.first()
        while (!table.eof()) {
          var pnode = Global.getTreeviewPNode(
            nodeMap,
            nodes,
            table.getValueByName('PID')
          )
          var node = {}
          // data
          node['id'] = table.getValueByName('ID')
          node['ip'] = table.getValueByName('IP')
          node['name'] = table.getValueByName('Name')
          node['text'] = Global.strFormat('{0} [{1}]', node.ip, node.name)
          node['icon'] = Global.getSystemIconStr(table.getValueByName('System'))
          node['type'] = 2
          pnode.push(node)
          table.next()
        }
        this.treeData = nodes
        // 数据回显
        if (this.params.computerName !== '') {
          for (let i = 0; i < nodes.length; i++) {
            if (nodes[i].type === 2) {
              if (
                Global.strFormat(
                  '{0}{1}{2}',
                  ',',
                  this.params.computerName,
                  ','
                ).indexOf(
                  Global.strFormat('{0}{1}{2}', ',', nodes[i].ip, ',')
                ) !== -1
              ) {
                this.treeDefaultCheckedKeys.push(nodes[i].id)
              }
            }
          }
        } else if (this.params.agentId !== '') {
          for (let i = 0; i < nodes.length; i++) {
            if (nodes[i].type === 2) {
              if (
                Global.strFormat(
                  '{0}{1}{2}',
                  ',',
                  this.params.agentId,
                  ','
                ).indexOf(
                  Global.strFormat('{0}{1}{2}', ',', nodes[i].id, ',')
                ) !== -1
              ) {
                this.treeDefaultCheckedKeys.push(nodes[i].id)
              }
            }
          }
        }
      })
    },
    confirm() {
      const nodes = this.$refs.tree.getCheckedNodes()
      const arrID = []
      const arrIP = []
      const arrName = []
      for (let i = 0; i < nodes.length; i++) {
        if (nodes[i].type === 2 && nodes[i].id !== '') {
          arrID.push(nodes[i].id)
          arrIP.push(nodes[i].ip)
          arrName.push(nodes[i].name)
        }
      }
      if (arrID.length === 0) return this.$message.warning('请选择机器。')
      const params = {
        agentId: arrID.toString(),
        agentName: arrName.toString(),
        agentIP: arrIP.toString()
      }
      if (this.params.confirm !== undefined) {
        this.params.confirm(params)
      } else {
        this.$emit('confirm', params)
      }
    }
  }
}
</script>

<style scoped>
</style>
